package com.punai.gk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punai.common.utils.SecurityUtils;
import com.punai.gk.domain.GkItem;
import com.punai.gk.mapper.GkItemMapper;
import com.punai.gk.service.IGkItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 归口字段定义Service业务层处理
 *
 * @author zxw_
 * @date 2023-01-06
 */
@Service
public class GkItemServiceImpl extends ServiceImpl<GkItemMapper,GkItem> implements IGkItemService
{
    @Autowired
    private GkItemMapper gkItemMapper;

    /**
     * 查询归口字段定义
     *
     *
     * @param templateId
     * @param id 归口字段定义主键
     * @return 归口字段定义
     */
    @Override
    public GkItem selectGkItemById(String templateId, String id)
    {
        return gkItemMapper.selectOne(
                Wrappers.<GkItem>lambdaQuery()
                        .eq(GkItem::getCompanyId, SecurityUtils.getCompanyId())
                        .eq(GkItem::getTemplateId, templateId)
                        .eq(GkItem::getId, id)
        );
    }

    /**
     * 查询归口字段定义列表
     *
     * @param gkItem 归口字段定义
     * @return 归口字段定义
     */
    @Override
    public List<GkItem> selectGkItemList(GkItem gkItem)
    {
        Long companyId = SecurityUtils.getCompanyId();
        return gkItemMapper.selectList(
                Wrappers.<GkItem>lambdaQuery()
                        .eq(GkItem::getCompanyId, companyId)
                .eq(GkItem::getTemplateId,gkItem.getTemplateId())
        );
    }

    /**
     * 新增归口字段定义
     *
     * @param gkItem 归口字段定义
     * @return 结果
     */
    @Override
    public int insertGkItem(GkItem gkItem)
    {
        gkItem.setCompanyId(SecurityUtils.getCompanyId());
        gkItem.setOrderNum(getMaxOrderNum(gkItem.getTemplateId()) + 1);
        return gkItemMapper.insert(gkItem);
    }


    private Integer getMaxOrderNum(String templateId) {
        return gkItemMapper.getMaxOrderNum(SecurityUtils.getCompanyId(),templateId);
    }

    /**
     * 修改归口字段定义
     *
     * @param gkItem 归口字段定义
     * @return 结果
     */
    @Override
    public int updateGkItem(GkItem gkItem)
    {
        GkItem newGkItem = new GkItem();
        newGkItem.setNameDesc(gkItem.getNameDesc());
        newGkItem.setTypeDesc(gkItem.getTypeDesc());
        newGkItem.setDefValue(gkItem.getDefValue());
        String typeDesc = gkItem.getTypeDesc();
        if ("SE".equals(typeDesc)) {
            newGkItem.setSelectValue(gkItem.getSelectValue());
        } else {
            newGkItem.setSelectValue("");
        }
        LambdaQueryWrapper<GkItem> updateWrapper = Wrappers.lambdaQuery();
        updateWrapper.eq(GkItem::getId,gkItem.getId());
        updateWrapper.eq(GkItem::getTemplateId,gkItem.getTemplateId());
        updateWrapper.eq(GkItem::getCompanyId,SecurityUtils.getCompanyId());
        return gkItemMapper.update(gkItem,updateWrapper);
    }

    /**
     * 批量删除归口字段定义
     *
     * @param ids 需要删除的归口字段定义主键
     * @return 结果
     */
    @Override
    public int deleteGkItemByIds(String[] ids)
    {
        return gkItemMapper.deleteGkItemByIds(ids);
    }

    /**
     * 删除归口字段定义信息
     *
     * @param id 归口字段定义主键
     * @return 结果
     */
    @Override
    public int deleteGkItemById(String templateId,String id)
    {
        return gkItemMapper.delete(
                Wrappers.<GkItem>lambdaQuery()
                        .eq(GkItem::getCompanyId, SecurityUtils.getCompanyId())
                        .eq(GkItem::getTemplateId, templateId)
                        .eq(GkItem::getId, id)
        );
    }

    @Override
    public String getMaxCode(String templateId) {
        Long companyId = SecurityUtils.getCompanyId();
        return gkItemMapper.selectMaxCode(companyId,templateId);
    }
}
